package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* AlarmClockView
* */
class AlarmClockView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_13D30C)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeWidth = context.resources.getDimension(R.dimen.dp_3)
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_13D30C)
        }
    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeWidth = context.resources.getDimension(R.dimen.dp_3)
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_20DCCC)
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy)
            drawCircle(
                0F,
                0F,
                cx * 7 / 10,
                mPaint0
            )

            mPath.apply {
                reset()
                val sin135 = sinValue(135.0)
                val con135 = conValue(135.0)
                moveTo((cx * 7 / 10) * con135, (cx * 7 / 10) * sin135)
                lineTo(cx * 9 / 10 * con135 , cx * 9 / 10 * sin135)
            }
            drawPath(mPath, mPaint1)

            mPath.apply {
                reset()
                val sin45 = sinValue(45.0)
                val con45 = conValue(45.0)
                moveTo((cx * 7 / 10) * con45, (cx * 7 / 10) * sin45)
                lineTo(cx * 9 / 10 * con45, cx * 9 / 10 * sin45)
            }
            drawPath(mPath, mPaint1)

            mPath.apply {
                reset()
                val sin0 = sinValue(215.0)
                val cos0 = conValue(215.0)
                moveTo(cx * 8 / 10 * cos0 ,cy * 8 / 10 * sin0)
                val sin1 = sinValue(235.0)
                val cos1 = conValue(235.0)
                lineTo(cx * 8 / 10 * cos1 ,cy * 8 / 10 * sin1)
            }
            drawPath(mPath, mPaint1)

            mPath.apply {
                reset()
                val sin0 = sinValue(305.0)
                val cos0 = conValue(305.0)
                moveTo(cx * 8 / 10 * cos0 ,cy * 8 / 10 * sin0)
                val sin1 = sinValue(325.0)
                val cos1 = conValue(325.0)
                lineTo(cx * 8 / 10 * cos1 ,cy * 8 / 10 * sin1)
            }
            drawPath(mPath, mPaint1)

            mPath.apply {
                reset()
                val sin45 = sinValue(45.0)
                val con45 = conValue(45.0)
                moveTo(0F, -cy * 7 / 20)
                lineTo(0F, 0F)
                lineTo((cx * 7 / 10) * con45 - cx * 5 / 20, (cx * 7 / 10) * sin45 - cy * 5 / 20)
            }
            drawPath(mPath, mPaint2)
        }
    }

    private fun conValue(angle: Double) : Float {
        return Math.cos(Math.toRadians(angle)).toFloat()
    }

    private fun sinValue(angle: Double) : Float {
        return Math.sin(Math.toRadians(angle)).toFloat()
    }
}